package com.csc775.gui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import com.csc775.message.ControlMessage;
import com.csc775.message.ControlRquestType;
import com.csc775.networking.Client;
import com.csc775.AndroidWalkieTalkieActivity;
import com.csc775.R;

/**
 * This class will display groups
 * @author pkulkar
 *
 */
public class GroupChooseActivity extends Activity {

	// private EditText text;
	private CheckBox checkbox1; // checkbox for group 1
	private CheckBox checkbox2; // checkbox for group 2
	private CheckBox checkbox3; // checkbox for group 3
	private CheckBox checkbox4; // checkbox for group 4
	Client c; // client to send message
	String grp1; // text for group1 checkbox
	String grp2; // text for group2 checkbox
	String grp3; // text for group3 checkbox
	String grp4; // text for group4 checkbox
	Boolean isChecked = false; // checks if checkbox is checked or not
	private StringBuffer buf; // buffer to append user response
	ControlMessage msg; // ControlMessage object
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.showgroups);
		checkbox1 = (CheckBox) findViewById(R.id.grp1);
		checkbox2 = (CheckBox) findViewById(R.id.grp2);
		checkbox3 = (CheckBox) findViewById(R.id.grp3);
		checkbox4 = (CheckBox) findViewById(R.id.grp4);
		Button join = (Button) findViewById(R.id.join);

		/**
		 * invoked when the user clicks the join button
		 */
		join.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent();
				setResult(RESULT_OK, intent);
				buf = new StringBuffer();
				// called if user selects 1st check box
				if (checkbox1.isChecked()) {
					buf.append("~");
					buf.append(checkbox1.getText());
					isChecked = true;

				}
				// called if user selects 2nd check box
				if (checkbox2.isChecked()) {
					buf.append("~");
					buf.append(checkbox2.getText());
					isChecked = true;
				}
				// called if user selects 3rd check box
				if (checkbox3.isChecked()) {
					buf.append("~");
					buf.append(checkbox3.getText());
					isChecked = true;
				}
				// called if user selects 4th check box
				if (checkbox4.isChecked()) {
					buf.append("~");
					buf.append(checkbox4.getText());
					isChecked = true;
				}

				// send selected groups info to server using ControlMessage
				// class
				// get the ack from server as register successfully
				// then only switch to talk activity.
				if (isChecked) {
					msg = new ControlMessage(
							AndroidWalkieTalkieActivity.serverIP,
							AndroidWalkieTalkieActivity.serverPort, buf
									.toString().length(), buf.toString());
					msg.setRequestType(ControlRquestType.JOIN);
					LoginActivity.getClient().Sendmessage(msg.toString());
					msg = LoginActivity.getClient().recevMsg();

					if (msg.getRequestType() == ControlRquestType.JOIN_ACK) {
						if (msg.getExtraData().equals("REG_SUCEESS")) {
							Intent myIntent = new Intent(view.getContext(),
									TalkActivity.class);
							startActivityForResult(myIntent, 0);
						} else {
							Toast toast = Toast.makeText(
									getApplicationContext(), "Unable to join",
									Toast.LENGTH_LONG);
							toast.show();

						}
					}
				} else {
					Toast toast = Toast.makeText(getApplicationContext(),
							"Please select atleast one group",
							Toast.LENGTH_LONG);
					toast.show();
					return;
				}
				finish(); // finishes the current activity

			}

		});

	}

}